package org.jeecg.modules.manage.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.mapper.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 统计[管理]
 * @Author: yfkj
 * @Date: 2023-02-23
 * @Version: V1.0
 */
@RestController
@RequestMapping("/manageApi/statistics")
@Slf4j
public class MStatisticsController {

    @Resource
    private MCaishuiRechargeMapper mCaishuiRechargeMapper;
    @Resource
    private MCaishuiTaskSettleMapper mCaishuiTaskSettleMapper;
    @Resource
    private MCaishuiBillMapper mCaishuiBillMapper;
    @Resource
    private MCaishuiMemberMapper mCaishuiMemberMapper;
    @Resource
    private MCaishuiBusinessMapper mCaishuiBusinessMapper;
    @Resource
    private MCaishuiServiceCompanyMapper mCaishuiServiceCompanyMapper;
    @Resource
    private MCaishuiPreItemsMapper mCaishuiPreItemsMapper;
    @Resource
    private MCaishuiRefundRecordMapper mCaishuiRefundRecordMapper;


    /**
     * 资金统计
     *
     * @return
     */
    @GetMapping(value = "/queryMoneyStatistics")
    public Result<?> queryMoneyStatistics() {
        JSONObject result = new JSONObject(8);
        //充值总额
        Map<String, String> params = new HashMap<>();
        params.put("status","1");
        Map<String,Object> map = mCaishuiRechargeMapper.queryTotalAmount(params);
        result.put("rechargeTotalAmount",oConvertUtils.getDouble(map.get("amount")+"",0.00));
        //充值总额（本月）
        params.put("dateType","by");
        map = mCaishuiRechargeMapper.queryTotalAmount(params);
        result.put("rechargeTotalAmountMonth",oConvertUtils.getDouble(map.get("amount")+"",0.00));

        //下发金额
        params = new HashMap<>();
        params.put("status","2");
        params.put("extendStatus","2");
        map = mCaishuiTaskSettleMapper.queryTaskSettleTotal(params);
        result.put("issuedTotalAmount",oConvertUtils.getDouble(map.get("successAmount")+"",0.00));
        //下发金额（本月）
        params.put("dateType","by");
        map = mCaishuiTaskSettleMapper.queryTaskSettleTotal(params);
        result.put("issuedTotalAmountMonth",oConvertUtils.getDouble(map.get("successAmount")+"",0.00));

        //开票金额
        params = new HashMap<>();
        params.put("status","7");
        map = mCaishuiBillMapper.queryBillTotal(params);
        result.put("billTotalAmount",oConvertUtils.getDouble(map.get("amount")+"",0.00));
        //开票金额（本月）
        params.put("dateType","by");
        map = mCaishuiBillMapper.queryBillTotal(params);
        result.put("billTotalAmountMonth",oConvertUtils.getDouble(map.get("amount")+"",0.00));

        //退款金额
        params = new HashMap<>();
        params.put("status","4");
        map = mCaishuiRefundRecordMapper.queryRefundTotal(params);
        result.put("refundAmount",oConvertUtils.getDouble(map.get("money")+"",0.00));
        //退款金额（本月）
        params.put("dateType","by");
        map = mCaishuiRefundRecordMapper.queryRefundTotal(params);
        result.put("refundAmountMonth",oConvertUtils.getDouble(map.get("money")+"",0.00));
        return Result.OK(result);
    }

    /**
     * 数量统计
     * @return
     */
    @GetMapping(value = "/queryNumberStatistics")
    public Result<?> queryNumberStatistics() {
        JSONObject result = new JSONObject(8);
        //人才数
        Map<String, String> params = new HashMap<>();
        params.put("signingStatus","2");
        result.put("memberNumber",mCaishuiMemberMapper.queryMemberTotal(params));
        //统计图数据
        result.put("memberNumberStatistics",mCaishuiMemberMapper.last7DaysMemberStatistics(params));

        //普通商户数
        params = new HashMap<>();
        params.put("type","1");
        params.put("status","1");
        params.put("isDefault","0");
        result.put("businessPNumber",mCaishuiBusinessMapper.queryBusinessTotal(params));
        //统计图数据
        result.put("businessPNumberStatistics",mCaishuiBusinessMapper.last7DaysBusinessStatistics(params));

        //渠道商户数
        params.put("type","3");
        result.put("businessQNumber",mCaishuiBusinessMapper.queryBusinessTotal(params));
        //统计图数据
        result.put("businessQNumberStatistics",mCaishuiBusinessMapper.last7DaysBusinessStatistics(params));

        //服务公司数
        params = new HashMap<>();
        params.put("status","1");
        result.put("companyNumber",mCaishuiServiceCompanyMapper.queryCompanyTotal(params));
        //统计图数据
        result.put("companyNumberStatistics",mCaishuiServiceCompanyMapper.last7DaysCompanyStatistics(params));
        return Result.OK(result);
    }

    /**
     * 预充值分组统计
     * @return
     */
    @GetMapping(value = "/queryPreItemGroupStatistics")
    public Result<?> queryPreItemGroupStatistics() {
        JSONObject result = new JSONObject(2);
        //查询预充值分组统计
        List<Map<String,Object>> list = mCaishuiPreItemsMapper.queryGroupStatistics();
        double preItemTotal = 0.00;
        List<Map<String,Object>> preItemStatistics = null;
        if (oConvertUtils.listIsNotEmpty(list)) {
            preItemStatistics = new ArrayList<>();
            for (Map<String, Object> map : list) {
                if (oConvertUtils.getInt(map.get("type"))==5) {
                    preItemTotal = oConvertUtils.getDouble(map.get("value")+"",0.00);
                }else {
                    preItemStatistics.add(map);
                }
            }
        }
        result.put("preItemTotal",preItemTotal);
        result.put("preItemStatistics",preItemStatistics);
        return Result.OK(result);
    }

    /**
     * 充值趋势
     * @param type 1-充值趋势，2-发放趋势，3开票趋势，3退款趋势
     * @param dateType 1-最近7天，2-最近30天，3-最近一年
     * @return
     */
    @GetMapping(value = "/queryChartStatistics")
    public Result<?> queryChartStatistics(@RequestParam(name = "type", required = true) int type,
                                             @RequestParam(name = "dateType", required = true) int dateType) {
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        switch (type) {
            case 1:
                params.put("status","1");
                if(dateType == 1){//最近7天
                    list = mCaishuiRechargeMapper.last7DaysRechargeStatistics(params);
                } else if(dateType == 2){//最近30天
                    list = mCaishuiRechargeMapper.last30DaysRechargeStatistics(params);
                } else if(dateType == 3){//最近一年
                    list = mCaishuiRechargeMapper.lastOneYearRechargeStatistics(params);
                }
                break;
            case 2:
                params.put("status","2");
                params.put("extendStatus","2");
                if(dateType == 1){//最近7天
                    list = mCaishuiTaskSettleMapper.last7DaysTaskSettleStatistics(params);
                } else if(dateType == 2){//最近30天
                    list = mCaishuiTaskSettleMapper.last30DaysTaskSettleStatistics(params);
                } else if(dateType == 3){//最近一年
                    list = mCaishuiTaskSettleMapper.lastOneYearTaskSettleStatistics(params);
                }
                break;
            case 3:
                params.put("status","7");
                if(dateType == 1){//最近7天
                    list = mCaishuiBillMapper.last7DaysBillStatistics(params);
                } else if(dateType == 2){//最近30天
                    list = mCaishuiBillMapper.last30DaysBillStatistics(params);
                } else if(dateType == 3){//最近一年
                    list = mCaishuiBillMapper.lastOneYearBillStatistics(params);
                }
                break;
            case 4:
                params.put("status","4");
                if(dateType == 1){//最近7天
                    list = mCaishuiRefundRecordMapper.last7DaysRefundStatistics(params);
                } else if(dateType == 2){//最近30天
                    list = mCaishuiRefundRecordMapper.last30DaysRefundStatistics(params);
                } else if(dateType == 3){//最近一年
                    list = mCaishuiRefundRecordMapper.lastOneYearRefundStatistics(params);
                }
                break;
            default:
                break;
        }
        return Result.OK(list);
    }

}
